import { fileURLToPath, URL } from "node:url"
import { defineConfig } from "vite"
import reactPlugin from "@vitejs/plugin-react"
import { createRequire } from "node:module"
import copy from "rollup-plugin-copy"
const require = createRequire(import.meta.url)
const pkg = require("./package.json")

const base = "/"

export default defineConfig(({ command, mode, ssrBuild }) => ({
  base,
  plugins: [
    reactPlugin(),
    copy({
      targets: [
        { src: "server/public", dest: "dist" },
        { src: "server/html", dest: "dist" }
      ],
      verbose: true,
      hook: "writeBundle"
    })
  ],
  build: {
    rollupOptions: {
      output: {
        preserveModules: true // 是否保持文件引用结构
      },
      external: [/node_modules/, "fsevents", ...Object.keys(pkg.dependencies), /\/dist\//, /\/public\//]
    }
  },
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url))
    }
  },
  define: {
    __VERSION__: JSON.stringify(process.env.npm_package_version)
  }
}))
